# coding=utf-8
__author__ = 'liubin'

import os
from flask.ext.admin import form
from sqlalchemy import Column, Integer, String, Text
from sqlalchemy.event import listens_for
from project import db, app
class Store(db.Model):
    """
    商店表
    """
    __tablename__ = 'store_tab'
    id = Column(Integer, primary_key=True)
    name = Column(String(50), nullable=False)
    description = Column(Text)
    image = Column(String(100))

    def __unicode__(self):
        return 'id:%s name:%s' % (self.id, self.name)


@listens_for(Store, 'after_delete')
def del_image(mapper, connection, target):
    if target.image:
        # Delete image
        try:
            os.remove(os.path.join(app.config['UPLOAD_PATH'], target.image).replace('\\', '/'))
        except OSError:
            pass

        # Delete thumbnail
        try:
            os.remove(os.path.join(app.config['UPLOAD_PATH'],
                              form.thumbgen_filename(target.image)))
        except OSError:
            pass